package com.sino.struts2;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.sino.business.IRoleService;
import com.sino.model.EntityrolerelationModel;
import com.sino.model.RoleModel;

@SuppressWarnings("serial")
public class RoleAction extends ActionSupport {
	private IRoleService iroleServiceImp;

	private List roleList;// 角色列表

	private int id;

	private String name;// 角色名称

	private List unAuthUserList;// 用户列表

	private List expertList;// 专家列表

	private List authUserList;// 授权用户列表

	private String[] authList = null;

	private int roleId;// 角色id
	
    private String userString;//用户string
	/**
	 * @return the roleList
	 */
	public List getRoleList() {
		return roleList;
	}

	/**
	 * @param roleList
	 *            the roleList to set
	 */
	public void setRoleList(List roleList) {
		this.roleList = roleList;
	}

	/**
	 * @param iroleService
	 *            the iroleService to set
	 */
	/**
	 * @param iroleServiceImp
	 *            the iroleServiceImp to set
	 */
	public void setIroleServiceImp(IRoleService iroleServiceImp) {
		this.iroleServiceImp = iroleServiceImp;
	}

	/**
	 * @return the id
	 */
	public int getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(int id) {
		this.id = id;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}


	/**
	 * @return the unAuthUserList
	 */
	public List getUnAuthUserList() {
		return unAuthUserList;
	}

	/**
	 * @param unAuthUserList
	 *            the unAuthUserList to set
	 */
	public void setUnAuthUserList(List unAuthUserList) {
		this.unAuthUserList = unAuthUserList;
	}


	/**
	 * @return the authUserList
	 */
	public List getAuthUserList() {
		return authUserList;
	}

	/**
	 * @param authUserList
	 *            the authUserList to set
	 */
	public void setAuthUserList(List authUserList) {
		this.authUserList = authUserList;
	}

	/**
	 * @return the authList
	 */
	public String[] getAuthList() {
		return authList;
	}

	/**
	 * @param authList
	 *            the authList to set
	 */
	public void setAuthList(String[] authList) {
		this.authList = authList;
	}

	/**
	 * @return the roleId
	 */
	public int getRoleId() {
		return roleId;
	}

	/**
	 * @param roleId
	 *            the roleId to set
	 */
	public void setRoleId(int roleId) {
		this.roleId = roleId;
	}
    
	/**
	 * @return the userString
	 */
	public String getUserString() {
		return userString;
	}

	/**
	 * @param userString the userString to set
	 */
	public void setUserString(String userString) {
		this.userString = userString;
	}

	
	
	
	/**
	 * @return the expertList
	 */
	public List getExpertList() {
		return expertList;
	}

	/**
	 * @param expertList the expertList to set
	 */
	public void setExpertList(List expertList) {
		this.expertList = expertList;
	}

	// 调用iroleServiceImp的listAllRoleModels函数列出所有的角色
	public String listRoles() {
		roleList = this.iroleServiceImp.listAllRoleModels();
		return SUCCESS;
	}

	// 先构造一个RoleModel，然后调用iroleServiceImp的saveRoleModel函数保存角色
	public String saveRole() {
		RoleModel rm = new RoleModel();
		rm.setName(name);
		this.iroleServiceImp.saveRoleModel(rm);
		return SUCCESS;
	}

	// 通过角色id调用iroleServiceImp的delRoleModel函数删除一个角色
	public String delRole() {
		String msg="";
		if(-1==this.iroleServiceImp.delRoleModel(id))
		{
			return SUCCESS;
		}
		else if(1==this.iroleServiceImp.delRoleModel(id))
		{
			msg="该角色已经被授权给用户，禁止删除";
		}
		else if(0==this.iroleServiceImp.delRoleModel(id))
		{
			msg="该角色已经被授权给专家，禁止删除";
		}
		else if(2==this.iroleServiceImp.delRoleModel(id))
		{
			msg="该角色已经被分配权限，禁止删除";
		}
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setContentType("text/html; charset=utf-8");
		try {
			PrintWriter out = response.getWriter();
			out
					.println("<script language='JavaScript' type='text/JavaScript'>");
			out.println("alert('"+msg+"');");
			out.println("location.href='listRoles.action';");	
			out.println("</script>");
		} catch (IOException e) {
			throw new RuntimeException("该角色已经被授权，禁止删除！");
		}
		return null;
	}

	// 获取表单数据，构造一个RoleModel，然后调用iroleServiceImp的updateRoleModel函数更新角色的名称
	public String updateRole() {
		RoleModel rm = new RoleModel();
		rm.setId(id);
		rm.setName(name);
		this.iroleServiceImp.updateRoleModel(rm);
		return SUCCESS;
	}

	/**
	 * 函数主要功能：转向快速授权用户界面
	 * 
	 * @return
	 */
	public String toAuthUser() {
		this.setRoleId(roleId);// 将角色id放入request中，以便后期调用
		authList = null;// 清空授权列表，以便接受表单提交过来的专家列表（如果不进行清空，容易造成专家权限的授予与删除不能正确执行）
		userString=this.iroleServiceImp.listUserString(roleId);
		return SUCCESS;
	}

	/**
	 * 函数主要功能：转向快速授权专家界面
	 * 
	 * @return
	 */
	public String toAuthExpert() {
		this.setRoleId(roleId);// 将角色id放入request中，以便后期调用
		authList = null;// 清空授权列表，以便接受表单提交过来的专家列表（如果不进行清空，容易造成专家权限的授予与删除不能正确执行）
		expertList=this.iroleServiceImp.listAllExpertForAuth(roleId);
		return SUCCESS;
	}

	/**
	 * 函数主要功能：调用iroleServiceImp的saveOrRevokeEntity功能授予或者回收用户的权限，调用完关闭当前页面
	 * 
	 * @return
	 */
	public String authUser() {
		this.iroleServiceImp.saveOrRevokeEntity(roleId,
				EntityrolerelationModel.ENTITY_USER, authList);//EntityrolerelationModel.ENTITY_USER表示当前实体为用户
		// 关闭当前页面
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html; charset=utf-8");
		try {
			PrintWriter out = response.getWriter();
			out
					.println("<script language='JavaScript' type='text/JavaScript'>");
			out.println("window.close();");
			out.println("</script>");
		} catch (IOException e) {
			throw new RuntimeException("授权用户异常！");
		}

		return null;
	}

	/**
	 * 函数主要功能：调用iroleServiceImp的saveOrRevokeEntity功能授予或者回收专家的权限，调用完关闭当前页面
	 * 
	 * @return
	 */
	public String authExpert() {

		this.iroleServiceImp.saveOrRevokeEntity(roleId,
				EntityrolerelationModel.ENTITY_EXPERT, authList);//EntityrolerelationModel.ENTITY_EXPERT表示当前实体为专家
		// 关闭当前页面
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html; charset=utf-8");
		try {
			PrintWriter out = response.getWriter();
			out
					.println("<script language='JavaScript' type='text/JavaScript'>");
			out.println("window.close();");
			out.println("</script>");
		} catch (IOException e) {
			throw new RuntimeException("授权专家异常！");
		}

		return null;
	}

}
